import { Link } from 'react-router-dom'
import { Card, Table, Tag, Space, Breadcrumb, Button, Popconfirm, message } from 'antd'
import { EditOutlined, DeleteOutlined } from '@ant-design/icons'
import FilterForm from './FilterForm'
import {useState,useEffect} from 'react'
import { useNavigate } from 'react-router-dom'
import http from '../../utils/http'
const Article = () => {
  const navigator = useNavigate()
  const [messageApi, contextHolder] = message.useMessage()
  const columns = [
    {
      title: '封面',
      dataIndex: 'cover',
      width: 120,
      render: cover => {
        return <img src={cover.images[0] || img404} width={80} height={60} alt="" />
      }
    },
    {
      title: '标题',
      dataIndex: 'title',
      width: 220
    },
    {
      title: '状态',
      dataIndex: 'status',
      render: data => {
        const status = { 0: '审核失败', 1: '待审核', 2: '审核中', 3: '审核通过', 4: '草稿' }
        const color = { 0: 'blue', 1: 'blue', 2: 'orange', 3: 'green', 4: 'gray' }
        return <Tag color={color[data]}>{status[data]}</Tag>
      }
    },
    {
      title: '发布时间',
      dataIndex: 'pubdate'
    },
    {
      title: '阅读数',
      dataIndex: 'read_count'
    },
    {
      title: '评论数',
      dataIndex: 'comment_count'
    },
    {
      title: '点赞数',
      dataIndex: 'like_count'
    },
    {
      title: '操作',
      render: data => {
        return (
          <Space size="middle">
            <Button type="primary" shape="circle" icon={<EditOutlined />}
            onClick={() => navigator(`/publish?id=${data.id}`)}
            />
            <Popconfirm
              title="确认删除该条文章吗?"
              onConfirm={() => delArticle(data)}
              okText="确认"
              cancelText="取消"
            >
              <Button
                type="primary"
                danger
                shape="circle"
                icon={<DeleteOutlined />}
              />
            </Popconfirm>
          </Space>
        )
      }
    }
  ]
  const [article, setArticleList] = useState({
    list: [],
    count: 0
  })
  const [params, setParams] = useState({
    page: 1,
    per_page: 4,
    begin_pubdate: null,
    end_pubdate: null,
    status: null,
    channel_id: null
  })
const [Formdata, setFormdata] = useState({
    status: '',
    channel_id: '推荐',
    data:null
})
  //获取频道列表
  const getArticleList = async () => {
    const res = await http.get('/mp/articles', {params})
    const { results, total_count } = res.data
    setArticleList({
      list: results,
      count: total_count
    })
   }
  useEffect(() => {
   getArticleList() 
  },[params])
  const Formclick = (values) => {
    setFormdata(values)
  }
  const delArticle = async (data) => {
    const res = await http.delete(`/mp/articles/${data.id}`)
    if(res.message !== 'OK') {
      return messageApi.open({
        type:'error',
        content: res.message, 
      })
    } 
    messageApi.open({
      type: 'success',
      content: '删除成功',
    })
    getArticleList()
}
  return (
    <div>
      {contextHolder}
      <Card
        title={
          <Breadcrumb items={[
            { title: <Link to={'/'}>首页</Link> },
            { title: '文章列表' },
          ]} />
        }
        style={{ marginBottom: 20 }}
      >
        <FilterForm onClick={Formclick} data={Formdata}/>
      </Card>
      <Card title={`根据筛选条件共查询到 ${article.count} 条结果：`}>
        <Table rowKey="id" columns={columns} dataSource={article.list} pagination={
          {
            current: params.page,
            pageSize: params.per_page,
            total: article.count, 
            onChange: (page, pageSize) => {
              setParams({...params, page, per_page: pageSize})
            }
          }
        }/>
      </Card>
    </div>
  )
}
export default Article
